package com.zekdot.password.action;

import com.zekdot.password.dto.Envelope;
import com.zekdot.password.entity.PasswordDataEntity;
import com.zekdot.password.entity.UserEntity;
import com.zekdot.password.service.PasswordDataService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Create by zekdot on 19-12-29.
 */
@Controller
@RequestMapping("/password")
public class PasswordDataAction {
    @Autowired
    private PasswordDataService passwordDataService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public String insertPassword(PasswordDataEntity passwordDataEntity, HttpSession session){
        UserEntity userEntity = (UserEntity) session.getAttribute("user");
        // 判断登录状态
        if(userEntity == null){
            return new Envelope(1024,"清先登录",null).toString();
        }
        // 存储密码
        return passwordDataService.insertPassword(passwordDataEntity,userEntity).toString();
    }

    @RequestMapping("/get")
    @ResponseBody
    public String getPasswordByPage(@RequestParam(defaultValue = "0") int lastId, @RequestParam(defaultValue = "10") int pageSize, HttpSession session){
        UserEntity userEntity = (UserEntity) session.getAttribute("user");
        // 判断登录状态
        if(userEntity == null){
            return new Envelope(1024,"清先登录",null).toString();
        }
        return passwordDataService.getPasswordByPage(lastId,pageSize,userEntity).toString();
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public String deletePassword(int id,HttpSession session){
        UserEntity userEntity = (UserEntity) session.getAttribute("user");
        // 判断登录状态
        if(userEntity == null){
            return new Envelope(1024,"清先登录",null).toString();
        }
        return passwordDataService.deletePassword(id,userEntity).toString();
    }

    @RequestMapping(value = "/search")
    @ResponseBody
    public String searchPassword(String keyword,@RequestParam(defaultValue = "0")int lastId,@RequestParam(defaultValue = "10") int pageSize,HttpSession session){
        UserEntity userEntity = (UserEntity) session.getAttribute("user");
        // 判断登录状态
        if(userEntity == null){
            return new Envelope(1024,"清先登录",null).toString();
        }
        return passwordDataService.searchPassword(keyword,lastId,pageSize,userEntity).toString();
    }
}
